package com.doctor.ui.medicalknowledge;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.doctor.adapter.KnowledgeSearchAdapter;
import com.doctor.bean.ArticleBean;
import com.doctor.bean.ArticleListBean;
import com.doctor.comm.ConstConfig;
import com.doctor.comm.LoadingTip;
import com.doctor.comm.MyApplication;
import com.doctor.comm.URLConfig;
import com.doctor.database.DbOperator;
import com.doctor.ui.BaseActivity;
import com.doctor.ui.R;

public class KnowledgeSearchActivity extends BaseActivity{

	/**
	 * 医学知识搜索结果
	 */
	
	private ListView listView;
	private List<ArticleBean> beanList;
	private TextView searchCount;
	private KnowledgeSearchAdapter adapter;
	private LinearLayout backBtn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.knowledge_search_activity);
		
		beanList=((ArticleListBean) getIntent().getSerializableExtra(ConstConfig.BEAN)).getDataList();
		initiaView();
	}
	
	@Override
	protected void initiaView() {
		// TODO Auto-generated method stub
		searchCount=(TextView) findViewById(R.id.searchCount);
		searchCount.setText("医学知识医疗技术  "+beanList.size()+"条相关搜索结果：");
		listView=(ListView) findViewById(R.id.listView);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				ArticleBean bean=(ArticleBean) arg1.getTag(R.id.bean_tag);
				String jj=bean.getJj();
				if (jj!=null && (bean.getDir()==54 || bean.getDir()==55)) {
					//视频
					int divide=jj.lastIndexOf("/");
					String medium=jj.substring(0, divide+1);
					String videoName=jj.substring(divide+1);
					String[] videoPart=videoName.split("\\.");
					String enCodeName = null;
					try {
						//中文需进行utf-8编码
						enCodeName=URLEncoder.encode(videoPart[0], "UTF-8");
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					File dir=new File(ConstConfig.VIDEO_FILE_OUT);
					if (!dir.exists()) {
						boolean result= dir.mkdirs();
						if (!result) {
							LoadingTip.showToast(KnowledgeSearchActivity.this, "请插入sd卡！");
							return;
						}
					}
					
					/**
					 * 先找外置存储卡里的视频再找内置存储卡里的视频
					 */
					String cache;
					File videoFile=new File(dir, videoName);
					if (!videoFile.exists()) {
						videoFile=new File(ConstConfig.VIDEO_FILE_INER,videoName);
						if (!videoFile.exists()) {
							LoadingTip.showToast(KnowledgeSearchActivity.this, "找不到该视频文件！");
							return;
						}
						cache=ConstConfig.VIDEO_FILE_INER+"/"+videoName;
					} else {
						cache=ConstConfig.VIDEO_FILE_OUT+"/"+videoName;
					}
					
					String videoUrl=URLConfig.WEBSITE.substring(0, URLConfig.WEBSITE.length()-1)+medium+enCodeName+"."+videoPart[1];
					Intent videoIntent=new Intent(KnowledgeSearchActivity.this,MyVideoPlayer.class);
					videoIntent.putExtra("url", videoUrl);
					videoIntent.putExtra("cache", cache);
					startActivity(videoIntent);
				} else {
					//医学文章
					Intent intent=new Intent(KnowledgeSearchActivity.this,ArticleActivity.class);
					int articleId=bean.getId();
					intent.putExtra(ConstConfig.ID, articleId);
					startActivity(intent);
				}
				
				//把该文章或视频的new标志清除掉
				if (bean.getIsNew()) {
					ContentValues values = new ContentValues();
					values.put("_is_new", 0);
					int result = DbOperator.getInstance(MyApplication.getInstance()).updateData(ConstConfig.ARTICLE_TABLE, values, bean.getId());
					if (result > 0) {
						bean.setIsNew(false);
						adapter.notifyDataSetChanged();
					}
				}
			}
		});
		
		adapter=new KnowledgeSearchAdapter(KnowledgeSearchActivity.this, beanList);
		listView.setAdapter(adapter);
		
		backBtn=(LinearLayout) findViewById(R.id.backBtn);
		backBtn.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				KnowledgeSearchActivity.this.finish();
			}
		});
	}
	
}
